<?php
//宝贝标签
defined('IN_TS') or die('Access Denied.');
$tagid = intval($_GET['tagid']);

$strTag = $db->once_fetch_assoc("select * from ".dbprefix."tag where tagid='$tagid'");

$page = isset($_GET['page']) ? intval($_GET['page']) : 1;

$url = SITE_URL.tsurl('group','topic_tag',array('tagid'=>$tagid,'page'=>''));

$lstart = $page*32-32;

$arrTagId = $db->fetch_all_assoc("select * from ".dbprefix."tag_topic_index where tagid='$tagid' limit $lstart,32");

$topic_num = $db->once_fetch_assoc("select count(topicid) from ".dbprefix."tag_topic_index where tagid='$tagid'");

$pageUrl = pagination($topic_num['count(topicid)'], 32, $page, $url);

foreach($arrTagId as $item){

	$strTopic = $db->once_fetch_assoc("select topicid,userid,groupid,title,count_comment,count_view,isphoto,isattach,addtime,uptime from ".dbprefix."group_topics where topicid = '".$item['topicid']."' order by uptime desc");
	$arrTopics[] = $strTopic;
	
}

foreach($arrTopics as $key=>$item){
	$arrTTopicsall[] = $item;
	$arrTTopicsall[$key]['user'] = aac('user')->getSimpleUser($item['userid']);
	$arrTTopicsall[$key]['group'] = $new['group']->getSimpleGroup('1');
}
	$userid = intval($TS_USER['user']['userid']);
	
	if( is_array($arrTTopicsall)){
			$i=0;
		foreach($arrTTopicsall as $key){
		$arrTTopics1[] = $arrTTopicsall[$i];
			$i=$i+4;
			}
		$arrTTopics1 =array_filter($arrTTopics1);
		if( is_array($arrTTopics1)){
		foreach($arrTTopics1 as $key=>$item){
			$arrTTopic1[] = $item;
			$arrTTopic1[$key]['user'] = aac('user',$db)->getSimpleUser($item['userid']);
			$thumbimg=$db->once_fetch_assoc("select * from ".dbprefix."thumb_topic_index where topicid=".$item['topicid']."");
			$last_return=$db->once_fetch_assoc("select * from ".dbprefix."group_topics_comments where topicid=".$item['topicid']." order by addtime desc limit 1");
			$arrTTopic1[$key]['last_return'] = $last_return['content'];
			$arrTTopic1[$key]['lastreturnuser'] = aac('user',$db)->getSimpleUser($last_return['userid']);
			$arrTTopic1[$key]['collects_num'] = $db->once_num_rows("select * from ".dbprefix."group_topics_collects where topicid=".$item['topicid']."");
$arrTTopic1[$key]['iscollect'] = $db->once_num_rows("select * from ".dbprefix."group_topics_collects where userid=".$userid." and topicid = ".$item['topicid']."");
			if ($thumbimg){
			$arrTTopic1[$key]['thumb'] =$thumbimg['indeximgurl'];
			}else{
			$random = random(14);
			$imgurl="cache/thumb/index/".$random."w200.jpg";
			$oldimgurl = $new['home']->getfristimg($item['content']);
			$arr_img=getimagesize($oldimgurl);
			$resizeimage = new resizeimage( "$oldimgurl", "200", "", "0", "$imgurl" );
			$tid=$item['topicid'];
			$db->query("INSERT INTO ".dbprefix."thumb_topic_index (`topicid`,`indeximgurl`) VALUES ('$tid','$imgurl')");
			$arrTTopic1[$key]['thumb'] =$imgurl;
			}
			$thumbimg=$db->once_fetch_assoc("select * from ".dbprefix."thumb_topic_topic where topicid=".$item['topicid']."");
			if ($thumbimg&&strstr($thumbimg['indeximgurl'],'t100x100')){
			}else{
			$random = random(14);
			$imgurl="cache/thumb/index/".$random."t100x100.jpg";
			$oldimgurl = $new['home']->getfristimg($item['content']);
			$arr_img=getimagesize($oldimgurl);
			$resizeimage = new resizeimage( "$oldimgurl", "100", "100", "1", "$imgurl" );
			$tid=$item['topicid'];
			$db->query("INSERT INTO ".dbprefix."thumb_topic_topic (`topicid`,`indeximgurl`) VALUES ('$tid','$imgurl')");
			}
		}
	}
	}
	
	if( is_array($arrTTopicsall)){
			$i=1;
		foreach($arrTTopicsall as $key){
		$arrTTopics2[] = $arrTTopicsall[$i];
			$i=$i+4;
			}
		$arrTTopics2 =array_filter($arrTTopics2);
		if( is_array($arrTTopics2)){
		foreach($arrTTopics2 as $key=>$item){
		$arrTTopic2[] = $item;
			$arrTTopic2[$key]['user'] = aac('user',$db)->getSimpleUser($item['userid']);
			$thumbimg=$db->once_fetch_assoc("select * from ".dbprefix."thumb_topic_index where topicid=".$item['topicid']."");
						$last_return=$db->once_fetch_assoc("select * from ".dbprefix."group_topics_comments where topicid=".$item['topicid']." order by addtime desc limit 1");
			$arrTTopic2[$key]['last_return'] = $last_return['content'];
			$arrTTopic2[$key]['lastreturnuser'] = aac('user',$db)->getSimpleUser($last_return['userid']);
			$arrTTopic2[$key]['collects_num'] = $db->once_num_rows("select * from ".dbprefix."group_topics_collects where topicid=".$item['topicid']."");
			$arrTTopic2[$key]['iscollect'] = $db->once_num_rows("select * from ".dbprefix."group_topics_collects where userid=".$userid." and topicid = ".$item['topicid']."");
			if ($thumbimg){
			$arrTTopic2[$key]['thumb'] =$thumbimg['indeximgurl'];
			}else{
			$random = random(14);
			$imgurl="cache/thumb/index/".$random."w200.jpg";
			$oldimgurl = $new['home']->getfristimg($item['content']);
			$arr_img=getimagesize($oldimgurl);
			$resizeimage = new resizeimage( "$oldimgurl", "200", "", "0", "$imgurl" );
			$tid=$item['topicid'];
			$db->query("INSERT INTO ".dbprefix."thumb_topic_index (`topicid`,`indeximgurl`) VALUES ('$tid','$imgurl')");
			$arrTTopic2[$key]['thumb'] =$imgurl;
			}
			$thumbimg=$db->once_fetch_assoc("select * from ".dbprefix."thumb_topic_topic where topicid=".$item['topicid']."");
			if ($thumbimg&&strstr($thumbimg['indeximgurl'],'t100x100')){
			}else{
			$random = random(14);
			$imgurl="cache/thumb/index/".$random."t100x100.jpg";
			$oldimgurl = $new['home']->getfristimg($item['content']);
			$arr_img=getimagesize($oldimgurl);
			$resizeimage = new resizeimage( "$oldimgurl", "100", "100", "1", "$imgurl" );
			$tid=$item['topicid'];
			$db->query("INSERT INTO ".dbprefix."thumb_topic_topic (`topicid`,`indeximgurl`) VALUES ('$tid','$imgurl')");
			}
		}
	}
	}
	
	if( is_array($arrTTopicsall)){
			$i=2;
		foreach($arrTTopicsall as $key){
		$arrTTopics3[] = $arrTTopicsall[$i];
			$i=$i+4;
			}
		$arrTTopics3 =array_filter($arrTTopics3);
		if( is_array($arrTTopics3)){
		foreach($arrTTopics3 as $key=>$item){
			$arrTTopic3[] = $item;
			$arrTTopic3[$key]['user'] = aac('user',$db)->getSimpleUser($item['userid']);
			$thumbimg=$db->once_fetch_assoc("select * from ".dbprefix."thumb_topic_index where topicid=".$item['topicid']."");
						$last_return=$db->once_fetch_assoc("select * from ".dbprefix."group_topics_comments where topicid=".$item['topicid']." order by addtime desc limit 1");
			$arrTTopic3[$key]['last_return'] = $last_return['content'];
			$arrTTopic3[$key]['lastreturnuser'] = aac('user',$db)->getSimpleUser($last_return['userid']);
			$arrTTopic3[$key]['collects_num'] = $db->once_num_rows("select * from ".dbprefix."group_topics_collects where topicid=".$item['topicid']."");
			$arrTTopic3[$key]['iscollect'] = $db->once_num_rows("select * from ".dbprefix."group_topics_collects where userid=".$userid." and topicid = ".$item['topicid']."");
			if ($thumbimg){
			$arrTTopic3[$key]['thumb'] =$thumbimg['indeximgurl'];
			}else{
			$random = random(14);
			$imgurl="cache/thumb/index/".$random."w200.jpg";
			$oldimgurl = $new['home']->getfristimg($item['content']);
			$arr_img=getimagesize($oldimgurl);
			$resizeimage = new resizeimage( "$oldimgurl", "200", "", "0", "$imgurl" );
			$tid=$item['topicid'];
			$db->query("INSERT INTO ".dbprefix."thumb_topic_index (`topicid`,`indeximgurl`) VALUES ('$tid','$imgurl')");
			$arrTTopic3[$key]['thumb'] =$imgurl;
			}
			$thumbimg=$db->once_fetch_assoc("select * from ".dbprefix."thumb_topic_topic where topicid=".$item['topicid']."");
			if ($thumbimg&&strstr($thumbimg['indeximgurl'],'t100x100')){
			}else{
			$random = random(14);
			$imgurl="cache/thumb/index/".$random."t100x100.jpg";
			$oldimgurl = $new['home']->getfristimg($item['content']);
			$arr_img=getimagesize($oldimgurl);
			$resizeimage = new resizeimage( "$oldimgurl", "100", "100", "1", "$imgurl" );
			$tid=$item['topicid'];
			$db->query("INSERT INTO ".dbprefix."thumb_topic_topic (`topicid`,`indeximgurl`) VALUES ('$tid','$imgurl')");
			}
		}
	}
	}
	
	if( is_array($arrTTopicsall)){
			$i=3;
		foreach($arrTTopicsall as $key){
		$arrTTopics4[] = $arrTTopicsall[$i];
			$i=$i+4;
			}
		$arrTTopics4 =array_filter($arrTTopics4);
		if( is_array($arrTTopics4)){
		foreach($arrTTopics4 as $key=>$item){
			$arrTTopic4[] = $item;
			$arrTTopic4[$key]['user'] = aac('user',$db)->getSimpleUser($item['userid']);
			$thumbimg=$db->once_fetch_assoc("select * from ".dbprefix."thumb_topic_index where topicid=".$item['topicid']."");
						$last_return=$db->once_fetch_assoc("select * from ".dbprefix."group_topics_comments where topicid=".$item['topicid']." order by addtime desc limit 1");
			$arrTTopic4[$key]['last_return'] = $last_return['content'];
			$arrTTopic4[$key]['lastreturnuser'] = aac('user',$db)->getSimpleUser($last_return['userid']);
			$arrTTopic4[$key]['collects_num'] = $db->once_num_rows("select * from ".dbprefix."group_topics_collects where topicid=".$item['topicid']."");
			$arrTTopic4[$key]['iscollect'] = $db->once_num_rows("select * from ".dbprefix."group_topics_collects where userid=".$userid." and topicid = ".$item['topicid']."");
			if ($thumbimg){
			$arrTTopic4[$key]['thumb'] =$thumbimg['indeximgurl'];
			}else{
			$random = random(14);
			$imgurl="cache/thumb/index/".$random."w200.jpg";
			$oldimgurl = $new['home']->getfristimg($item['content']);
			$arr_img=getimagesize($oldimgurl);
			$resizeimage = new resizeimage( "$oldimgurl", "200", "", "0", "$imgurl" );
			$tid=$item['topicid'];
			$db->query("INSERT INTO ".dbprefix."thumb_topic_index (`topicid`,`indeximgurl`) VALUES ('$tid','$imgurl')");
			$arrTTopic4[$key]['thumb'] =$imgurl;
			}
			$thumbimg=$db->once_fetch_assoc("select * from ".dbprefix."thumb_topic_topic where topicid=".$item['topicid']."");
			if ($thumbimg&&strstr($thumbimg['indeximgurl'],'t100x100')){
			}else{
			$random = random(14);
			$imgurl="cache/thumb/index/".$random."t100x100.jpg";
			$oldimgurl = $new['home']->getfristimg($item['content']);
			$arr_img=getimagesize($oldimgurl);
			$resizeimage = new resizeimage( "$oldimgurl", "100", "100", "1", "$imgurl" );
			$tid=$item['topicid'];
			$db->query("INSERT INTO ".dbprefix."thumb_topic_topic (`topicid`,`indeximgurl`) VALUES ('$tid','$imgurl')");
			}
		}
	}
	}

//热门tag
$arrTag = $db->fetch_all_assoc("select * from ".dbprefix."tag order by count_topic desc limit 30");

$title = $strTag['tagname'];

include template("topic_tag");